package security.config;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.xml.NamespaceHandler;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import security.config.parser.RBACBeanDefinitionParser;
import security.debug.RBACLogger;

public class SecurityNamespaceHandler implements NamespaceHandler {
	
	//private final Map<String, BeanDefinitionParser> parses = new HashMap<String, BeanDefinitionParser>();
	private final RBACBeanDefinitionParser rbacParser = new RBACBeanDefinitionParser();

	@Override
	public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition,
			ParserContext pc) {
		if (RBACLogger.debug()) {
			RBACLogger.log("SecurityNamespaceHandler#decorate is called!");
		}
		return null;
	}

	@Override
	public void init() {
		if (RBACLogger.debug()) {
			RBACLogger.log("SecurityNamespaceHandler#init is called!");
		}
	}

	@Override
	public BeanDefinition parse(Element element, ParserContext pc) {
		// TODO Auto-generated method stub
		String tagName = pc.getDelegate().getLocalName(element);
		if (!tagName.equals(rbacParser.getTagName())) {
			// Error
			if (RBACLogger.debug()) {
				RBACLogger.log("Element named: " + tagName + " is not supported!");
			}
		}
		if (RBACLogger.debug()) {
			RBACLogger.log("Element named: " + tagName + " is parsing by SecurityNamespaceHandler!");
		}
		return rbacParser.parse(element, pc);
	}

}
